package com.ruicar.afs.cloud.apply.commission.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.apply.commission.service.ApplyToCommissionService;
import com.ruicar.afs.cloud.bizcommon.commission.dto.basecommission.DealerinfoDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.basecommission.GetbasecontractlistDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.basecommission.ReceiptDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.currency.ConfirmSendMailDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.reward.ConfirmExpressDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.reward.ConfirmReceiveMoneyDto;
import com.ruicar.afs.cloud.bizcommon.commission.dto.reward.GetContractListDto;
import com.ruicar.afs.cloud.bizcommon.commission.enums.InterfaceModeUpload;
import com.ruicar.afs.cloud.bizcommon.commission.feign.BasecommissionFeign;
import com.ruicar.afs.cloud.bizcommon.commission.feign.DiscountmainFeign;
import com.ruicar.afs.cloud.bizcommon.commission.feign.QuarterrewardFeign;
import com.ruicar.afs.cloud.bizcommon.commission.feign.RewardFeign;
import com.ruicar.afs.cloud.bizcommon.commission.service.impl.CommissionServiceImpl;
import com.ruicar.afs.cloud.common.core.constant.SecurityConstants;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 贷额奖励类
 * @author panhj
 * @date 2020/6/11
 */
@Service
@AllArgsConstructor
@Slf4j
public class ApplyToCommissionServiceImpl implements ApplyToCommissionService {
    private RewardFeign rewardFeign;
    private BasecommissionFeign basecommissionFeign;
    private QuarterrewardFeign quarterrewardFeign;
    private DiscountmainFeign discountmainFeign;
    CommissionServiceImpl commissionService;
    @Override
    public IResponse getstatus() {
        //结果解析只有成功时，方可返回正确的值

        JSONObject json = basecommissionFeign.getstatus();
        if(0 == json.getInteger("code")){
            return IResponse.success(json);
        }else{
            return IResponse.fail(json.getString("msg"));
        }
    }
    @Override
    public IResponse rewardtypes() {
        //结果解析只有成功时，方可返回正确的值

        JSONObject json = rewardFeign.rewardtypes();
        if(0 == json.getInteger("code")){
            return IResponse.success(json);
        }else{
            return IResponse.fail(json.getString("msg"));
        }
    }

    @Override
    public JSONObject confirmReceiveMoney(ConfirmReceiveMoneyDto dto) {
        JSONObject json = rewardFeign.confirmreceivemoney(dto);
        return json;
    }
    @Override
    public JSONObject rebateReceiptMoney(ReceiptDto dto) {
        JSONObject json = basecommissionFeign.confirmreceivemoney(dto);
        return json;
    }
    @Override
    public JSONObject receiptMoney(ConfirmReceiveMoneyDto dto) {
        JSONObject json = quarterrewardFeign.receipt(dto);
        return json;
    }

    @Override
    public JSONObject getbasedealerinfo(DealerinfoDto dealerinfoDto) {
        JSONObject json = basecommissionFeign.getbasedealerinfo(dealerinfoDto);
        return json;
    }

    /**
     * 上传文件
     * @param file 文件
     * @param id 贴息列表ID
     */
    @Override
    public void uploadFile(MultipartFile file, String id) {
        //上传附件
        JSONObject json =  commissionService.uploadFile(file,InterfaceModeUpload.discountmainUploadImage.getValue());
        //经销商回执确认
        JSONObject json1 = new JSONObject();
        json1.put("id",id);
        json1.put("filepath",json.get("data"));
        discountmainFeign.confirm(json1);
    }

    @Override
    public JSONObject getDiscountInfo(DealerinfoDto dealerinfoDto) {
        JSONObject json = discountmainFeign.info(dealerinfoDto);
        return json;
    }

    @Override
    public JSONObject getContractList(GetContractListDto dto) {
        JSONObject json = rewardFeign.getcontractlist(dto);
        return json;
    }
    @Override
    public JSONObject getBaseContractList(GetbasecontractlistDto dto) {
        JSONObject json = basecommissionFeign.getbasecontractlist(dto);
        return json;
    }
    @Override
    public JSONObject getDiscountList(GetbasecontractlistDto dto) {
        JSONObject json = discountmainFeign.discontlist(dto);
        return json;
    }

    @Override
    public JSONObject confirmExpress(ConfirmExpressDto dto) {
        JSONObject json = rewardFeign.confirmexpress(dto);
        return json;
    }

    @Override
    public JSONObject confirmSendMail(ConfirmSendMailDto dto) {
        JSONObject json = quarterrewardFeign.confirmsendmail(dto);
        return json;
    }

    @Override
    public JSONObject rebateConfirmExpress(ConfirmExpressDto dto) {
        JSONObject json = basecommissionFeign.confirmexpress(dto);
        return json;
    }
    @Override
    public JSONObject getBaseDealerInfo(DealerinfoDto dto) {
        JSONObject json = basecommissionFeign.getbasedealerinfo(dto);
        return json;
    }

    @Override
    public IResponse currencyRewardTypes() {
        //结果解析只有成功时，方可返回正确的值

        JSONObject json = quarterrewardFeign.rewardList();
        if(0 == json.getInteger("code")){
            return IResponse.success(json);
        }else{
            return IResponse.fail(json.getString("msg"));
        }
    }

    @Override
    public IResponse getQuarter() {
        //结果解析只有成功时，方可返回正确的值

        JSONObject json = quarterrewardFeign.quarterList();
        if(0 == json.getInteger("code")){
            return IResponse.success(json);
        }else{
            return IResponse.fail(json.getString("msg"));
        }
    }
    /**
     * 获取请求头
     * @return
     */
    public static Map makeHeader(){
        Map headers = new HashMap<>();
        headers.put(SecurityConstants.AFS_CLEAR,SecurityConstants.AFS_CLEAR);
        headers.put("Content-Type", "application/json");
        return headers;
    }
}
